function getDictLabel(type, value, defaultLabel) {
  if ((!value && value !== 0) || (!type && type !== 0)) {
    if (defaultLabel !== undefined) {
      return defaultLabel;
    } else {
      return "--";
    }
  }
  const dictList = JSON.parse(sessionStorage.getItem("dictList") || "[]");
  const dicts = dictList[type];
  if (dicts) {
    for (let i = 0; i < dicts.length; i++) {
      if (dicts[i].value && dicts[i].value.toString() === value.toString()) {
        return dicts[i].label;
      }
    }
  }
  if (defaultLabel !== undefined) {
    return defaultLabel;
  } else {
    return "--";
  }
}

function getDictValue(type, label, defaultValue) {
  if ((!label && label !== 0) || (!type && type !== 0)) {
    if (defaultValue !== undefined) {
      return defaultValue;
    } else {
      return "--";
    }
  }
  const dictList = JSON.parse(sessionStorage.getItem("dictList") || "[]");
  const dicts = dictList[type];
  if (dicts) {
    for (let i = 0; i < dicts.length; i++) {
      if (dicts[i].label && dicts[i].label.toString() === label.toString()) {
        return dicts[i].value;
      }
    }
  }
  if (defaultValue !== undefined) {
    return defaultValue;
  } else {
    return "--";
  }
}

function getDictList(type) {
  if (!type && type !== 0) {
    return [];
  }
  const dictList = JSON.parse(sessionStorage.getItem("dictList") || "[]");
  const dicts = dictList[type];
  return dicts || [];
}

// 从字典列表找字典label
function getDictLabelByList(code, dictList, defaultValue) {
  let label = defaultValue || "";
  const dict = dictList.find((val) => val.value == code);
  if (dict) {
    label = dict.label;
  }
  return label;
}

export default { getDictLabel, getDictValue, getDictList, getDictLabelByList };
